package com.reset.password.util;

import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.File;
import java.util.List;
import java.util.Map;

/**
 * @Author pl711@qq.com
 * @PackageName reset-password
 * @Package com.reset.password.util
 * @Date 2024/11/5 15:53
 * @Version 1.0
 */


@Component //后续工具类被调用的时候，方便依赖注入
public class EmailUtil {

    private final JavaMailSender mailSender;
    private final String from;

    private final TemplateEngine templateEngine;

    @Autowired
    public EmailUtil(JavaMailSender mailSender,
                     @Value("${spring.mail.from}") String from,
                     TemplateEngine templateEngine) {
        this.mailSender = mailSender;
        this.from = from;
        this.templateEngine = templateEngine;
    }
    /**
     * 发送纯文本邮件信息
     * @param to      接收方
     * @param subject 邮件主题
     * @param templateName 模板名称
     * @param variables 模板变量
     */
    public void sendMessage(String to, String subject,  String templateName, Map<String, Object> variables) {
        Context context = new Context();
        context.setVariables(variables);

        // 渲染模板
        String content = templateEngine.process(templateName, context);

        // 创建邮件消息
        MimeMessage msg = mailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(msg, true, "UTF-8");
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content, true); // true 表示HTML内容
        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }

        // 发送邮件
        mailSender.send(msg);
    }


    /**
     * 发送带1个附件的邮件信息
     * @param to      接收方
     * @param subject 邮件主题
     * @param content 邮件内容（发送内容）
     * @param file 单个文件
     */
    public void sendMessage(String to, String subject, String content, File file) {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
            helper.setFrom(from); // 设置发送方
            helper.setTo(to); // 设置接收方
            helper.setSubject(subject); // 设置邮件主题
            helper.setText(content); // 设置邮件内容
            helper.addAttachment(file.getName(), file); // 添加附件（单个）
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        // 发送邮件
        mailSender.send(mimeMessage);
    }


    /**
     * 发送带多个附件的邮件信息
     * @param to      接收方
     * @param subject 邮件主题
     * @param content 邮件内容（发送内容）
     * @param files 多个文件
     */
    public void sendMessage(String to, String subject, String content, List<File> files) {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
            helper.setFrom(from); // 设置发送发
            helper.setTo(to); // 设置接收方
            helper.setSubject(subject); // 设置邮件主题
            helper.setText(content); // 设置邮件内容
            if (files != null && files.size() > 0) { // 添加附件（多个）
                for (File file : files) {
                    helper.addAttachment(file.getName(), file);
                }
            }
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        // 发送邮件
        mailSender.send(mimeMessage);
    }
}